class GroupsController < ApplicationController

# Begin authentication
  attr_reader :actions_to_check
  def initialize
    @actions_to_check = {:destroy => ['owner'],
                         :edit => ['owner'],
                         :show => ['own', 'private'],
                         :update_ajax => ['owner']}
  end
# End
  def index
    list
    @user =  session[:user]
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
#    @group_pages, @groups = paginate :groups, :conditions => ["groups.private = 0"], :include => [:images], :per_page => 24
    @group_pages, @groups = paginate :groups, :conditions => ["groups.user_id = ?", Goldberg.user.id], :include => [:images], :per_page => 24
  end

  def list_ajax
    @group_pages, @groups = paginate :groups, :conditions => ["groups.user_id = ?", Goldberg.user.id], :include => [:images], :per_page => 5
    render :partial => 'list_ajax', :layout => 'dialog'
  end

  def move
    params[:selected_ids].split(",").each do |id|
        image = Image.find_by_id(id)
        image.group_id = params[:group_id]
        image.save
    end
    redirect_to :controller => 'images', :action => 'list', :group_id => params[:old_group_id]
  end

  def update_ajax
    @group = Group.find(params[:id])
    eval("@group.#{params[:field_name]} = params[:value]")
    if (!@group.valid?)
      errors = @group.errors.on(params[:field_name])
      if errors.type == Array
        flash[:error] = errors.map{|e| params[:field_name]+' '+ e+'.'}.join('<br />')
      else
        flash[:error] = params[:field_name] + ' ' + errors
      end
      @group.reload
    else
      @group.save
    end
    @new_value = eval("@group.#{params[:field_name]}")
    render :layout => 'ajax'
  end

  def show
    if params[:id]!=nil   #list all images of the group
        @image_pages, @images = paginate :images, :conditions => ["group_id = ?", params[:id]], :per_page => 18
        @group = Group.find_by_id(params[:id])
    else
        flash[:notice] = "Please choose the image group you would like to view."
        redirect_to :controller => :groups, :action => :list
    end

    if request.xhr?
      render :partial => 'images'
    end
  end

  def new
    @group = Group.new
    render :layout => false
  end

  def create
    group_params ={:name => params[:group][:name],:user_id => Goldberg.user.id}

    @group = Group.new(group_params)
    if @group.save
      flash[:notice] = 'Group was successfully created.'
      redirect_to :controller => 'groups', :action => 'show', :id => @group.id
    else
      render :action => 'new'
    end
  end


  def edit
    @group = Group.find(params[:id])
  end

  def update
    @group = Group.find(params[:id])
    if @group.update_attributes(params[:group])
      flash[:notice] = 'Group was successfully updated.'
      redirect_to :action => 'show', :id => @group
    else
      render :action => 'edit'
    end
  end

  def destroy
    Group.find(params[:id]).destroy
    flash[:notice] = 'Group deleted successfully'
    redirect_to :action => 'list'
  end

  def new_group_ajax
    puts params.inspect
    if params[:group_id] == "New Group"
        render :partial => "form"
    else
        render :layout => false
    end
  end
end
